//
//  tieZiTwoTableViewCell.m
//  SXHomeTownClub
//
//  Created by 晋商行 on 2018/5/30.
//  Copyright © 2018年 jsx. All rights reserved.
//

#import "tieZiTwoTableViewCell.h"
#import "scrollCollectionViewCell.h"

@implementation tieZiTwoTableViewCell

- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
        [self setUpScrollCollectionView];
        
    }
    return self;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    
    [self.cellContentView addSubview:self.scrollCollectionView];
    self.scrollCollectionView.sd_layout.topSpaceToView(self.cellContentView, 0).rightSpaceToView(self.cellContentView, 0).leftSpaceToView(self.cellContentView, 0).bottomSpaceToView(self.cellContentView, 0);
    
    //注册通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(tongzhi:)name:@"thumb" object:nil];
}

- (void)tongzhi:(NSNotification *)text{
    self.dataSource =self.thumb;
    [self.scrollCollectionView reloadData];
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

#pragma mark --店铺网格
- (void)setUpScrollCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    
    if (!self.scrollCollectionView) {
        self.scrollCollectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0,  kScreenWidth - 40, 200) collectionViewLayout:flowLayout];
        //注册cell单元格
        [self.scrollCollectionView registerNib:[UINib nibWithNibName:@"scrollCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"scrollCell"];
        self.scrollCollectionView.scrollEnabled =NO;
        self.scrollCollectionView.delegate = self;
        self.scrollCollectionView.dataSource = self;
        self.scrollCollectionView.showsHorizontalScrollIndicator =NO;
        self.scrollCollectionView.backgroundColor =[UIColor whiteColor];
    }
}
/**
 *  UICollectionViewCell的个数
 */
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataSource.count;
}

/**
 *  每个UICollectionView展示的内容
 */
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * CellIdentifier = @"scrollCell";
    scrollCollectionViewCell *scrollCell  = [collectionView dequeueReusableCellWithReuseIdentifier:CellIdentifier forIndexPath:indexPath];
    [scrollCell.shopImage sd_setImageWithURL:[NSURL URLWithString:self.dataSource[indexPath.row]] placeholderImage:[UIImage imageNamed:PlaceHolderImg_Group]];
    
    return scrollCell;
}

/**
 *  定义每个UICollectionView 的大小
 */
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat width =(kScreenWidth-40) / 3;
    return CGSizeMake(width,width * 0.7);
}
/**
 *  定义每个UICollectionView 的 margin
 */
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0.0, 0.0, 0.0, 0.0);
}
/**
 *  定义每个UICollectionView 的横向间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
/**
 *  定义每个UICollectionView 的 竖向间距
 */
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}
#pragma mark --UICollectionViewDelegate
/**
 *  UICollectionView被选中时调用的方法
 */
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    MWPhotoBrowser *photoBrowser =[[MWPhotoBrowser alloc] initWithPhotos:self.pic];
    photoBrowser.delegate =self;
    [photoBrowser setCurrentPhotoIndex:indexPath.row];
    //push到MWPhotoBrowser
    [[self getCurrentVC].navigationController pushViewController:photoBrowser animated:YES];
}
//返回图片个数
- (NSUInteger)numberOfPhotosInPhotoBrowser:(MWPhotoBrowser *)photoBrowser{
    return self.pic.count;
}

//返回图片模型
- (id <MWPhoto>)photoBrowser:(MWPhotoBrowser *)photoBrowser photoAtIndex:(NSUInteger)index{
    
    //创建图片模型
    MWPhoto *photo = [MWPhoto photoWithURL:self.pic[index]];
    
    return photo;
    
}
//获取当前页面的导航栏视图
- (UIViewController *)getCurrentVC
{
    UIViewController *result = nil;
    
    UIWindow * window = [[UIApplication sharedApplication] keyWindow];
    if (window.windowLevel != UIWindowLevelNormal)
    {
        NSArray *windows = [[UIApplication sharedApplication] windows];
        for(UIWindow * tmpWin in windows)
        {
            if (tmpWin.windowLevel == UIWindowLevelNormal)
            {
                window = tmpWin;
                break;
            }
        }
    }
    
    UIView *frontView = [[window subviews] objectAtIndex:0];
    
    id nextResponder = [frontView nextResponder];
    
    if ([nextResponder isKindOfClass:[UIViewController class]])
        result = nextResponder;
    else
        result = window.rootViewController;
    
    return result;
}
@end
